<template>

  <div class="login_container">
    <!--<particles-bg type="lines" :bg="true" />-->

    <!--登录窗口-->
    <div class="login_box">

      <!--登录头像-->
      <div class="avatar">
        <img src="../assets/img/2.jpg" alt="">
      </div>
      <!--登录表单-->
      <div class="login_form">
        <el-form :model="loginForm" :rules="rules" ref="loginForm" label-width="70px" class="demo-ruleForm"
                 hide-required-asterisk>
          <el-form-item label="" prop="username">
            <el-input v-model="loginForm.username" placeholder="请输入用户名" prefix-icon="el-icon-user-solid"></el-input>
          </el-form-item>
          <el-form-item label="" prop="password">
            <el-input type="password" v-model="loginForm.password" prefix-icon="el-icon-lock"
                      placeholder="请输入密码"></el-input>
          </el-form-item>
          <el-form-item class="login_button_box">
            <el-button type="primary" @click="submitForm('loginForm')">登录</el-button>
            <el-button @click="resetForm('ruleForm')">注册</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>

  </div>
</template>


<style lang="less">

.login_container {
  height: 100%;
  width: 100%;
  background-image: url("../assets/img/1.jpg");
  background-repeat: no-repeat;
  background-size: 100% 100%;

  .login_box {
    width: 450px;
    height: 300px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    .avatar {
      width: 110px;
      height: 110px;
      border: 1px solid #EEEEEE;
      border-radius: 50%;
      margin: -55px auto;
      position: absolute;
      left: 35%;
      background-color: #EEEEEE;
      opacity: 0.8;

      img {
        width: 110px;
        height: 110px;
        border-radius: 50%;
      }
    }

    .login_form {
      position: absolute;
      bottom: 0;
      width: 100%;
      padding: 0 20px;
      box-sizing: border-box;


      .el-input {
        width: 250px;
      }
    }
  }
}


</style>


<script>
export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '密码', trigger: 'blur'}
        ],
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$http({
            url: '/login',
            method: 'post',
            data: {
              username: this.loginForm.username,
              password: this.$md5(this.loginForm.password)
            }
          }).then(({data}) => {
            if (data.code === 0) {
              localStorage.removeItem("user");
              localStorage.setItem('user', JSON.stringify(data.user)) //对象要经过序列化才可以存到localStorage
              console.log("userId"+JSON.parse(localStorage.getItem("user")).id) //localStorage中的对象要经过解析（反序列化）才可以被取出
              this.$router.push('/main')
              this.$notify({
                title: '成功',
                message: '登录成功',
                type: 'success',
                offset: 75
              });
            } else {
              this.$message.error(data.msg);
            }
          }).catch()

        } else {
          this.$message.error('请输入正确的账号信息！');
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
}
</script>
